﻿#include "FileIO.h"
QString readFile(QString filePath, QString section)
{
    QString result;
    QFile file(filePath);
    QList<QString> list;
    if( file.exists() )
    {
        if( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
        {
            QTextStream in(&file);
            while( !in.atEnd() )
            {
                list << in.readLine();
            }
            for(int i=0; i<list.count(); i++)
            {
                if( list[i].contains(section) )
                {
                    result = list[i].split("=",QString::SkipEmptyParts).at(1).simplified();
                }
            }
        }
        else
        {
            result = "";
        }
    }
    else
    {
        result = "";;
    }
    return result;
}

bool writeFile(QString filePath, QString section, QString replace)
{
    bool ret = true;
    QFile file(filePath);
    QList<QString> list;
    if( file.exists() )//如果文件存在
    {
        if( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
        {
            QTextStream in(&file);
            while(!in.atEnd())
            {
                 list << in.readLine();
            }

            for(int i=0; i<list.count(); i++)
            {
                if( list[i].contains(section))
                {
                    if( !replace.isEmpty() )
                    {
                        int length = replace.length();
                        list[i].resize(section.length()+1 + length);
                        list[i].replace(section.length()+1, length, replace.simplified()).trimmed();
                    }
                    else
                    {
                        ret = false;
                    }
                }
            }
            file.close();
        }

        if( file.open( QIODevice::WriteOnly | QIODevice::Text ))
        {
            for(int i=0; i<list.count(); i++)
            {
                file.write((list[i]).toLocal8Bit().constData());
                file.write("\r\n");
            }
            file.close();
        }
        else
        {
            ret = false;
        }
    }
    else
    {
        ret = false;
    }
    return ret;
}

QSqlDatabase onInitAccount()
{
    QFile file("/usr/local/sanway/dbTest/tcuaccount.db");
    QSqlDatabase accountDb;
    if( file.exists() )
    {
        if( QSqlDatabase::contains("qt_sql_default_connection") )
        {
            accountDb = QSqlDatabase::database("qt_sql_default_connection");
        }
        else
        {
            accountDb  = QSqlDatabase::addDatabase("QSQLITE");
        }
        accountDb.setDatabaseName("/usr/local/sanway/dbTest/tcuaccount.db");
        accountDb.close();
        if( !accountDb.open() )
        {
            printf("打开充电记录数据库失败!!!!!\n");
        }
        else
        {
            printf("打开充电记录数据库成功!!!!!\n");
        }
    }
    else
    {
        printf("充电记录数据库不存在");
    }
    return accountDb;
}
void onExprotFile(QString pathName, QSqlQueryModel* accountModel)
{
    QFile file(pathName);
    if( !file.open(QIODevice::WriteOnly | QIODevice::Text ) )
    {
         return;
    }

    QTextStream out(&file);
    out << QString("枪编号") << qSetFieldWidth(10);//0
    out << QString("上报情况") << qSetFieldWidth(10);//1
    out << QString("流水号") << qSetFieldWidth(36);//2
    out << QString("起始时间") << qSetFieldWidth(16);//3
    out << QString("结束时间") << qSetFieldWidth(16);//4
    out << QString("充电时间") << qSetFieldWidth(16);//5
    out << QString("支付卡号") << qSetFieldWidth(16);//6
    out << QString("用 户 号") << qSetFieldWidth(16);//7
    out << QString("卡   号") << qSetFieldWidth(16);//8
    out << QString("模型ID") << qSetFieldWidth(16);//9
    out << QString("停止原因") << qSetFieldWidth(18);//10
    out << QString("分时计费标志") << qSetFieldWidth(18);//11
    out << QString("充电开始值(总)") << qSetFieldWidth(18);//12
    out << QString("充电开始值(尖)") << qSetFieldWidth(18);//13
    out << QString("充电开始值(峰)") << qSetFieldWidth(18);//14
    out << QString("充电开始值(平)") << qSetFieldWidth(18);//15
    out << QString("充电开始值(谷)") << qSetFieldWidth(18);//16
    out << QString("充电结束值(总)") << qSetFieldWidth(18);//17
    out << QString("充电结束值(尖)") << qSetFieldWidth(18);//18
    out << QString("充电结束值(峰)") << qSetFieldWidth(18);//19
    out << QString("充电结束值(平)") << qSetFieldWidth(18);//20
    out << QString("充电结束值(谷)") << qSetFieldWidth(18);//21
    out << QString("本次充电量(总)") << qSetFieldWidth(18);//22
    out << QString("本次充电量(尖)") << qSetFieldWidth(18);//23
    out << QString("本次充电量(峰)") << qSetFieldWidth(18);//24
    out << QString("本次充电量(平)") << qSetFieldWidth(18);//25
    out << QString("本次充电量(谷)") << qSetFieldWidth(18);//26
    out << QString("本次充电电费(总)") << qSetFieldWidth(18);//27
    out << QString("本次充电电费(尖)") << qSetFieldWidth(18);//28
    out << QString("本次充电电费(峰)") << qSetFieldWidth(18);//29
    out << QString("本次充电电费(平)") << qSetFieldWidth(18);//30
    out << QString("本次充电电费(谷)") << qSetFieldWidth(18);//31
    out << QString("当前服务费") << qSetFieldWidth(18);//32
    out << QString("占位费") << qSetFieldWidth(18);//33
    out << QString("尖费率") << qSetFieldWidth(18);//34
    out << QString("峰费率") << qSetFieldWidth(18);//35
    out << QString("平费率") << qSetFieldWidth(18);//36
    out << QString("谷费率") << qSetFieldWidth(18);//37
    out << QString("服务费单价") << qSetFieldWidth(18);//38
    out << QString("停车费单价") << qSetFieldWidth(18);//39
    out << QString("预约费单价") << qSetFieldWidth(18);//40
    out << QString("电动汽标识") << qSetFieldWidth(18);//41
    out << endl;

    for(int row=0 ;row <accountModel->rowCount(); row++)
    {
        out <<  qSetFieldWidth(0) << accountModel->data(accountModel->index(row, 0)).toString() << qSetFieldWidth(14); //
        if( accountModel->data(accountModel->index(row, 1)).toInt() == 170 )
        {
            out <<  "已上报" << qSetFieldWidth(41);
        }
        else if( accountModel->data(accountModel->index(row, 1)).toInt() == 102 )
        {
            out <<  "未上报" << qSetFieldWidth(41);
        }
        out <<  reverse( accountModel->data(accountModel->index(row, 2)).toString() ) << qSetFieldWidth(20); //
        out <<  accountModel->data(accountModel->index(row, 3)).toString().left(10).insert(2,"-").insert(5,"-").insert(8, " ").insert(11,":") << qSetFieldWidth(20); //
        out <<  accountModel->data(accountModel->index(row, 4)).toString().left(10).insert(2,"-").insert(5,"-").insert(8, " ").insert(11,":") << qSetFieldWidth(10); //
        out <<  accountModel->data(accountModel->index(row, 5)).toFloat() << qSetFieldWidth(31); //
        out <<  accountModel->data(accountModel->index(row, 6)).toString() << qSetFieldWidth(20); //
        out <<  reverse( accountModel->data(accountModel->index(row, 7)).toString()) << qSetFieldWidth(19); //
        out <<  accountModel->data(accountModel->index(row, 8)).toString() << qSetFieldWidth(19); //
        out <<  accountModel->data(accountModel->index(row, 9)).toString() << qSetFieldWidth(5); //
        out <<  accountModel->data(accountModel->index(row, 10)).toInt() << qSetFieldWidth(20); //停止原因
        out <<  accountModel->data(accountModel->index(row, 11)).toString() << qSetFieldWidth(26); //分时计费标志
        out <<  accountModel->data(accountModel->index(row, 12)).toFloat()/100 << qSetFieldWidth(21); //充电开始值(总)
        for(int i=13; i<=26; i++)
        {
            out <<  accountModel->data(accountModel->index(row, i)).toFloat()/100 << qSetFieldWidth(24); //充电开始值(尖)
        }
        out <<  accountModel->data(accountModel->index(row, 27)).toFloat()/100 << qSetFieldWidth(28); //
        out <<  accountModel->data(accountModel->index(row, 28)).toString() << qSetFieldWidth(22); //当前服务费
        out <<  accountModel->data(accountModel->index(row, 29)).toString() << qSetFieldWidth(30); //
        for(int i=30; i<=32; i++)
        {
            out <<  accountModel->data(accountModel->index(row, i)).toString() << qSetFieldWidth(23);
        }
        out <<  accountModel->data(accountModel->index(row, 33)).toString() << qSetFieldWidth(26); //
        out <<  accountModel->data(accountModel->index(row, 34)).toString() << qSetFieldWidth(21); //
        out <<  accountModel->data(accountModel->index(row, 35)).toString() << qSetFieldWidth(21); //
        out <<  accountModel->data(accountModel->index(row, 36)).toString() << qSetFieldWidth(21); //
        out <<  accountModel->data(accountModel->index(row, 37)).toString() << qSetFieldWidth(18); //
        out <<  accountModel->data(accountModel->index(row, 38)).toString() << qSetFieldWidth(24); //
        out <<  accountModel->data(accountModel->index(row, 39)).toString() << qSetFieldWidth(22); //
        out <<  accountModel->data(accountModel->index(row, 40)).toString() << qSetFieldWidth(50); //
        out <<  accountModel->data(accountModel->index(row, 41)).toString() << qSetFieldWidth(50); //
        out << endl;
    }
    file.close();

}
//字符串的倒置;
QString reverse(QString str)
{
    QString ret = "";
    QList<QString> list;
    for(int i=0; i<str.length(); i=i+2)
    {
        list.append(str.mid(i, 2));
    }
    for(int i=0; i < str.length()/4; i++)
    {
        list.swap(i, str.length()/2-i-1);
    }
    for(int i=0; i<str.length()/2; i++)
    {
        ret.append(list.at(i));
    }
    return ret;
}
